package com.sync.data.job;

import cn.hutool.core.bean.BeanUtil;
import com.sync.data.ievent.entity.IeventSysUser;
import com.sync.data.ievent.repository.IeventSysUserRepository;
import com.sync.data.job.department.DepartmentBatchJob;
import com.sync.data.job.hcp.SpeakerAndHcpBatchJob;
import com.sync.data.job.user.SysUserSyncBatchJob;
import com.sync.data.mce.entity.MceSysUser;
import com.sync.data.mce.repository.MceSysUserRepository;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class JobService {

    @Autowired
    private JobLauncher jobLauncher;


    private final Job mtMtBudgetSyncJob;
    private final Job departmentSyncJob;
    private final Job speakerBatchJob;
    private final Job userSyncJob;
    @XxlJob("xxlSyncUsers")
    public ReturnT<String> xxlSyncUsers(String param) {
        return getStringReturnT(param, userSyncJob);
    }

    @XxlJob("xxlSyncHcpAndSpeaker")
    public ReturnT<String> xxlSyncHcpAndSpeaker(String param) {
        return getStringReturnT(param, speakerBatchJob);
    }


    @XxlJob("xxlSyncDepartment")
    public ReturnT<String> xxlSyncDepartment(String param) {

        return getStringReturnT(param, departmentSyncJob);
    }


    @XxlJob("xxlMtBudget")
    public ReturnT<String> xxlMtBudget(String param) {
        return getStringReturnT(param, mtMtBudgetSyncJob);
    }


    private ReturnT<String> getStringReturnT(String param, Job speakerBatchJob) {
        try {
            JobParameters jobParameters = new JobParametersBuilder()
                    .addLong("startTime", System.currentTimeMillis())
                    .addString("inputFile", param) // 使用传入的参数
                    .toJobParameters();

            JobExecution execution = jobLauncher.run(speakerBatchJob, jobParameters);

            XxlJobHelper.log("Batch job completed with status: {}", execution.getStatus());

            return execution.getStatus() == BatchStatus.COMPLETED ?
                    ReturnT.SUCCESS : ReturnT.FAIL;

        } catch (Exception e) {
            XxlJobHelper.log("Batch job execution failed: {}", e.getMessage());
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        }
    }

}
